<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    
    public function index(){
        //轮播图
        $carousel = M('carousel');
        $map = $carousel -> select();

        //产品
        $product = M('product');
        // $data = $product -> where('id=') select();
        $data = array('id'=>'1','age'=>'3');
        // var_dump($data);
        //尝试redis使用
        //set存 get取
        $redis = new \Redis();
        $redis->connect('127.0.0.1',6379);
        // $aa = $redis->set($str,json_encode($data));
        // $nn = json_decode($redis->get($str),true);
        //     foreach ($nn as $key => $value) {
        //         if($value==3){
        //             $ne[$key]==$value+1;
        //             var_dump($ne[$key]);
        //             continue;
        //         }
                // var_dump($value);
            // }
            // $redis->set($str,json_encode($nn));
            // $nns = json_decode($redis->get($str),true);
            // var_dump($nns);
        //队列
        $ss = 'awwd';
        $redis -> rpush($ss,json_encode(['name'=>'wangpeng','age'=>'24']));
        $redis -> rpush($ss,json_encode(['name'=>'xh','age'=>'23']));
        $redis -> rpush($ss,json_encode(['name'=>'ai','age'=>'xxx']));
        // $redis -> rpush($ss,json_encode(['name'=>'yy','age'=>'1314']));
        
        $saa = $redis -> lpop($ss);
        $st = $redis -> lrange($ss,0,-1);
        
        // var_dump($st);
        $redis -> del($ss);
        // var_dump(json_decode($nn));
        
        //计算器
        // $redis -> set($socre,1);

        // $scor = $redis -> decr($socre,5);
        // // var_dump($scor);
        // $scor = $redis -> INCR($socre);
        // var_dump($scor);

        //有序集合存储数据
        // $add='add';
        // $next=1;
        // $rext=5;
        // $redis -> zadd($add,$next,'wang');
        // $redis -> zadd($add,$rext,'peng');
        // $av = $redis -> zrevrange($add,0,-1,true);

        // var_dump($av);
        
        //哈希存取
        // $daa='dee';
        // $redis -> hset($daa,$dae,json_encode(['name'=>'wangpeng','age'=>'23']));
        // $dee = $redis -> hget($daa,$dae);
        // var_dump($dee);

        
        
        //新闻
        $notice = M('notice');
        $news = $notice -> select();

        $this -> assign('ad',$st);
        $this -> assign('map',$map);
        $this -> assign('data',$data);
        $this -> assign('news',$news);
        $this -> display();
    }
    
    //预约
    public function increase(){
        $about = M('about');
        $data['username'] = I('post.username');
        $data['phone'] = trim(I('post.phone'));
        $data['firsttime'] = time();
        $yzm = I('post.yzm');
        $syzm = session('yzm');
        if($yzm != $syzm){
            echo 3;
            return ;
        }
        session('yzm',null);
        $res = $about -> add($data);
        if($res){
            echo 1;
        }else{
            echo 2;
        }
    }
    //发送验证码
    public function yu(){
        include_once("ThinkPHP/Library/Think/SDK/CCPRestSDK.php");

        //主帐号
        $accountSid= '8aaf07085861d3a101586242f66901aa';

        //主帐号Token
        $accountToken= 'e3c321d3787b4093a68a31f6f425a050';

        //应用Id
        $appId='8aaf07085861d3a101586242f71f01b0';

        //请求地址，格式如下，不需要写https://
        $serverIP='app.cloopen.com';

        //请求端口 
        $serverPort='8883';

        //REST版本号
        $softVersion='2013-12-26';
        
        $phone = trim(I('post.phone'));
        $txyzm = I('post.txyzm');
        $verify = new \Think\Verify();
        if(!$verify -> check($txyzm)){
            echo 1;
            return;
        }
        
        $rand = rand(1000,9999);
        session('yzm',$rand);
        if($phone){
        $add = $this->sendTemplateSMS($phone,array($rand,'10'),"181062",$accountSid,$accountToken,$appId,$serverIP,$serverPort,$softVersion);//手机号码，替换内容数组，模板ID
        
        }
    }

    function sendTemplateSMS($to,$datas,$tempId)
    {

         // 初始化REST SDK
        global $accountSid,$accountToken,$appId,$serverIP,$serverPort,$softVersion;
        
          $serverIP='app.cloopen.com';
           $serverPort='8883';
           $softVersion='2013-12-26';
           //主帐号
        $accountSid= '8aaf07085861d3a101586242f66901aa';

        //主帐号Token
        $accountToken= 'e3c321d3787b4093a68a31f6f425a050';

        //应用Id
        $appId='8aaf07085861d3a101586242f71f01b0';
         $rest = new \Think\SDK\REST($serverIP,$serverPort,$softVersion);
         $rest->setAccount($accountSid,$accountToken);
         $rest->setAppId($appId);
        
         // 发送模板短信
         echo "Sending TemplateSMS to $to <br/>";
         $result = $rest->sendTemplateSMS($to,$datas,$tempId);
         if($result == NULL ) {
             echo "result error!";
             break;
         }
         if($result->statusCode!=0) {
             echo "error code :" . $result->statusCode . "<br>";
             echo "error msg :" . $result->statusMsg . "<br>";
             //TODO 添加错误处理逻辑
         }else{
             echo "Sendind TemplateSMS success!<br/>";
             // 获取返回信息
             $smsmessage = $result->TemplateSMS;
             echo "dateCreated:".$smsmessage->dateCreated."<br/>";
             echo "smsMessageSid:".$smsmessage->smsMessageSid."<br/>";
             //TODO 添加成功处理逻辑
         }
    }

    public function nesn(){
        $this -> display();
    }
}